<template>
	<view>
		<view class="header">
			<view class="title">员工统计</view>
		</view>

		<view class="content">
			<!-- 搜索区域 -->
			<view class="search-section">
				<view class="search-wrapper">
					<view class="search-column">
						<view class="search-item">
							<picker mode="date" :value="startDate" @change="onStartDateChange">
								<view class="picker">{{startDate || '请选择开始时间'}}</view>
							</picker>
						</view>
						<view class="search-item">
							<picker mode="date" :value="endDate" @change="onEndDateChange">
								<view class="picker">{{endDate || '请选择结束时间'}}</view>
							</picker>
						</view>
					</view>
					<button class="search-btn" @click="searchData">查询</button>
				</view>
			</view>

			<scroll-view scroll-y="true" @scrolltolower="loadData" class="scroll-view">
				<view v-for="(item, index) in staffList" :key="index">
					<view class="staff-card">
						<view class="card-header">
							<view class="user-info">
								<view class="rank-tag">{{index + 1}}</view>
								<image :src="item.avatar" class="avatar"></image>
								<text class="nickname">{{item.nickname}}</text>
							</view>
						</view>
						<view class="card-content">
							<view class="stats-section">
								<view class="section-title">直推数据</view>
								<view class="stats-grid">
									<view class="stats-item">
										<text class="value highlight">¥{{item.directAmount || '0.00'}}</text>
										<text class="label">金额</text>
									</view>
									<view class="stats-item">
										<text class="value">{{item.directOrders || 0}}</text>
										<text class="label">订单数</text>
									</view>
									<view class="stats-item">
										<text class="value">{{item.directUsers || 0}}</text>
										<text class="label">用户数</text>
									</view>
								</view>
							</view>
							<view class="divider"></view>
							<view class="stats-section">
								<view class="section-title">溯源数据</view>
								<view class="stats-grid">
									<view class="stats-item">
										<text class="value highlight">¥{{item.sourceAmount || '0.00'}}</text>
										<text class="label">金额</text>
									</view>
									<view class="stats-item">
										<text class="value">{{item.sourceOrders || 0}}</text>
										<text class="label">订单数</text>
									</view>
									<view class="stats-item">
										<text class="value">{{item.sourceUsers || 0}}</text>
										<text class="label">用户数</text>
									</view>
								</view>
							</view>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import * as api from '@/api/staff'
	import storage from '@/utils/storage'
	
	export default {
		data() {
			return {
				activityId: 0,
				startDate: '',
				endDate: '',
				staffList: []
			}
		},
		onLoad() {
			this.activityId = storage.get('activityId')
			this.loadData()
		},
		methods: {
			onStartDateChange(e) {
				this.startDate = e.detail.value
			},
			onEndDateChange(e) {
				this.endDate = e.detail.value
			},
			searchData() {
				this.loadData()
			},
			loadData() {
				api.getStaffStats({
					activity_id: this.activityId,
					start_time: this.startDate,
					end_time: this.endDate
				}).then(res => {
					this.staffList = res.data.map(item => ({
						nickname: item.nickname,
						avatar: item.avatar_url,
						directAmount: item.direct_order_amount,
						directOrders: item.direct_order_count,
						directUsers: item.direct_user_count,
						sourceAmount: item.trace_order_amount,
						sourceOrders: item.trace_order_count,
						sourceUsers: item.trace_user_count
					}))
				})
			}
		}
	}
</script>

<style>
	page {
		background-color: #F6F7FB;
	}

	.header {
		background-color: #FFAC3E;
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
		height: 250rpx;
		border-radius: 0rpx 0rpx 20rpx 20rpx;
	}

	.header > .title {
		color: #fff;
		padding: 30rpx;
		font-size: 40rpx;
		font-weight: bold;
	}

	.header > .bottonGroup {
		width: 100%;
		position: initial;
		display: inline-table;
		text-align: center;
		margin-top: 0px;
	}

	.header > .bottonGroup view {
		font-size: 28rpx;
		float: left;
		padding: 5px 10px;
		margin-right: 5px;
		margin: 0px 0px 10px 10px;
		z-index: 999;
	}

	.header > .bottonGroup .botton {
		color: #FFFFFF;
	}

	.header > .bottonGroup .focus {
		border-bottom: 3px solid #FFFFFF;
		color: #FFFFFF;
	}

	.content {
		position: relative;
		margin-top: 110rpx;
		padding: 20rpx;
		box-sizing: border-box;
	}

	.search-section {
		background-color: #fff;
		border-radius: 16rpx;
		padding: 20rpx;
		margin-bottom: 20rpx;
	}

	.search-wrapper {
		display: flex;
		align-items: stretch;
	}
	
	.search-column {
		flex: 1;
		margin-right: 20rpx;
	}
	
	.search-item {
		margin-bottom: 20rpx;
	}
	
	.search-item:last-child {
		margin-bottom: 0;
	}
	
	.picker {
		height: 88rpx;
		line-height: 88rpx;
		background-color: #f5f5f5;
		border-radius: 8rpx;
		padding: 0 30rpx;
		font-size: 28rpx;
		color: #333;
	}
	
	.search-btn {
		width: 120rpx;
		background-color: #FFAC3E;
		color: #fff;
		font-size: 32rpx;
		border-radius: 8rpx;
		border: none;
		flex-shrink: 0;
		display: flex;
		align-items: center;
		justify-content: center;
	}
	
	.search-btn:active {
		opacity: 0.8;
	}

	.scroll-view {
		height: calc(100vh - 110rpx - 200rpx);
	}

	.staff-card {
		width: 100%;
		border-radius: 30rpx;
		background-color: #fff;
		margin-bottom: 20rpx;
		padding: 20rpx 25rpx;
		box-sizing: border-box;
	}

	.card-header {
		display: flex;
		align-items: center;
		padding-bottom: 20rpx;
	}

	.user-info {
		display: flex;
		align-items: center;
	}

	.rank-tag {
		width: 40rpx;
		height: 40rpx;
		background: #FFAC3E;
		color: #fff;
		border-radius: 20rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 24rpx;
		margin-right: 20rpx;
	}

	.avatar {
		width: 80rpx;
		height: 80rpx;
		border-radius: 50%;
		margin-right: 20rpx;
	}

	.nickname {
		font-size: 32rpx;
		color: #333;
		font-weight: 500;
	}

	.card-content {
		padding-top: 20rpx;
	}

	.stats-section {
		padding: 20rpx 0;
	}

	.section-title {
		font-size: 28rpx;
		color: #666;
		margin-bottom: 20rpx;
	}

	.stats-grid {
		display: flex;
		justify-content: space-between;
	}

	.stats-item {
		flex: 1;
		display: flex;
		flex-direction: column;
		align-items: center;
	}

	.stats-item .value {
		font-size: 32rpx;
		color: #333;
		font-weight: 500;
		margin-bottom: 8rpx;
	}

	.stats-item .value.highlight {
		color: #FF6B6B;
	}

	.stats-item .label {
		font-size: 24rpx;
		color: #999;
	}

	.divider {
		height: 1rpx;
		background-color: #f5f5f5;
		margin: 0 -25rpx;
	}
</style> 